{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "41081334",
   "metadata": {},
   "source": [
    "## 集合\n",
    "会保留下来唯一的那些元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "86be1844",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{123, 456, 789}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=[123,123,456,789]\n",
    "tang = set(tang)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "22daee86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang = set()\n",
    "type(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a2d08a4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{123, 456, 789}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang = set([123,123,456,456,789])\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a6f16bfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang ={1,1,1,3,4}\n",
    "tang"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14af5ce0",
   "metadata": {},
   "source": [
    "## 集合操作\n",
    "并集\n",
    "交集\n",
    "包含"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "41effb4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "a={1,2,3,4}\n",
    "b={2,3,4,5}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "df5e1ee3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.union(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "01f149a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.union(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "39088b07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a|b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e4debaee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.intersection(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7c4c1fa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a & b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "148ce76b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.difference(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "59f8f256",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.difference(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "eb36b0d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a -b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4cc53897",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ecd000e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "a ={1,2,3,4,5,6}\n",
    "b ={2,3,4}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "32e068f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.issubset(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dca3f144",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.issubset(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "991797a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b <=a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6991ad6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b >a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4b47aa31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a <=a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "52899415",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a < a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "065f9367",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ={1,2,3}\n",
    "a.add(4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f9475336",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.update([4,5,6])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7a67f202",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.remove(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "c20c676e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ac4743d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5, 6}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.pop()\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5cde59bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5, 6}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e851caa8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
